<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2005 by CPG-Nuke Dev Team
  http://www.dragonflycms.com

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version
**********************************************/
if (!defined('ADMIN_MOD_INSTALL')) { exit; }

class Forums extends Installer_Module {

	# COMMON

	/** $blocks
	Install with admin defaults blocks when true, or none at all when false. */
	public $blocks = true;

	/** $bypass
	Try to bypass most SQL errors. */
	public $bypass = false;

	# ADVANCED

	/** $is_instance_of
	The parent module, if any. */
	public $is_instance_of = '';

	/**
	Must change $prefix when installing a not-instance copy, something short eg bba, bbb, bbc, bbd ... */
	public $prefix = 'bb';

	# DEVELOPERS
	/**	$test
	Output most queries for debug, enable $bypass if any issue. */
	public $test = false;

	public $author = 'CPG-Nuke Dev Team';
	public $config = array();
	public $dbtables;
	public $description = 'CPG Bulletin Board by CPG-Nuke Dev Team and based on phpBB 2.x which is released under the GNU GPL';
	public $modname = 'CPG-BB';
	public $radmin = true;
	public $userconfig = array();
	public $version = '3.0.4';
	public $website = 'dragonflycms.org';


	public function __construct()
	{
		$this->prefix = $this->is_instance_of ? strtolower(__CLASS__).'_' : $this->prefix;

		$this->dbtables = array(
			$this->prefix.'auth_access',
			$this->prefix.'categories',
			$this->prefix.'forum_prune',
			$this->prefix.'forums',
			$this->prefix.'posts',
			$this->prefix.'posts_text',
			$this->prefix.'search_wordlist',
			$this->prefix.'search_wordmatch',
			$this->prefix.'topic_icons',
			$this->prefix.'topics',
			$this->prefix.'topics_watch',
			$this->prefix.'vote_desc',
			$this->prefix.'vote_results',
			$this->prefix.'vote_voters',
			$this->prefix.'words',
			$this->prefix.'attachments',
			$this->prefix.'attachments_desc'
		);
		if (!$this->is_instance_of) {
			$this->dbtables[] = $this->prefix.'attachments_config';
			$this->dbtables[] = $this->prefix.'attach_quota';
			$this->dbtables[] = $this->prefix.'config';
			$this->dbtables[] = $this->prefix.'disallow';
			$this->dbtables[] = $this->prefix.'extension_groups';
			$this->dbtables[] = $this->prefix.'extensions';
			$this->dbtables[] = $this->prefix.'forbidden_extensions';
			$this->dbtables[] = $this->prefix.'quota_limits';
		}
	}

	public function pre_install() { return true; }

	function post_install()
	{
		if (!$this->is_instance_of) {
			$this->add_query('INSERT', $this->prefix.'config', "'allow_html', '0'");
			$this->add_query('INSERT', $this->prefix.'config', "'allow_html_tags', 'b,i,u,pre'");
			$this->add_query('INSERT', $this->prefix.'config', "'allow_bbcode', '1'");
			$this->add_query('INSERT', $this->prefix.'config', "'allow_smilies', '1'");
			$this->add_query('INSERT', $this->prefix.'config', "'allow_sig', '1'");
			$this->add_query('INSERT', $this->prefix.'config', "'posts_per_page', '15'");
			$this->add_query('INSERT', $this->prefix.'config', "'topics_per_page', '50'");
			$this->add_query('INSERT', $this->prefix.'config', "'hot_threshold', '25'");
			$this->add_query('INSERT', $this->prefix.'config', "'max_poll_options', '10'");
			$this->add_query('INSERT', $this->prefix.'config', "'flood_interval', '15'");
			$this->add_query('INSERT', $this->prefix.'config', "'default_dateformat', 'D M d, Y g:i a'");
			$this->add_query('INSERT', $this->prefix.'config', "'prune_enable', '0'");
			$this->add_query('INSERT', $this->prefix.'config', "'board_startdate', '".time()."'");

			$this->add_query('INSERT', $this->prefix.'attachments_config', "'upload_dir','uploads/forums'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'upload_img','images/icons/icon_disk.gif'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'topic_icon','images/icons/icon_clip.gif'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'display_order','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'max_filesize','262144'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'attachment_quota','52428800'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'max_attachments','3'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'disable_mod','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'attachment_topic_review','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'allow_ftp_upload','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'show_apcp','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'attach_version','2.3.9'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'default_upload_quota', '0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'ftp_server',''");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'ftp_path',''");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'download_path',''");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'ftp_user',''");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'ftp_pass',''");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'ftp_pasv_mode','1'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'img_display_inlined','1'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'img_max_width','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'img_max_height','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'img_link_width','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'img_link_height','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'img_create_thumbnail','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'img_min_thumb_filesize','12000'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'wma_autoplay','0'");
			$this->add_query('INSERT', $this->prefix.'attachments_config', "'flash_autoplay','0'");

			# -- forbidden_extensions
			$this->add_query('INSERT', $this->prefix.'forbidden_extensions', "1,'php'");
			$this->add_query('INSERT', $this->prefix.'forbidden_extensions', "2,'php3'");
			$this->add_query('INSERT', $this->prefix.'forbidden_extensions', "3,'php4'");
			$this->add_query('INSERT', $this->prefix.'forbidden_extensions', "4,'phtml'");
			$this->add_query('INSERT', $this->prefix.'forbidden_extensions', "5,'pl'");
			$this->add_query('INSERT', $this->prefix.'forbidden_extensions', "6,'asp'");
			$this->add_query('INSERT', $this->prefix.'forbidden_extensions', "7,'cgi'");
			# -- extension_groups
			$this->add_query('INSERT', $this->prefix.'extension_groups', "1,'Images',1,1,1,'',0,''");
			$this->add_query('INSERT', $this->prefix.'extension_groups', "2,'Archives',0,1,1,'',0,''");
			$this->add_query('INSERT', $this->prefix.'extension_groups', "3,'Plain Text',0,0,1,'',0,''");
			$this->add_query('INSERT', $this->prefix.'extension_groups', "4,'Documents',0,0,1,'',0,''");
			$this->add_query('INSERT', $this->prefix.'extension_groups', "5,'Real Media',0,0,2,'',0,''");
			$this->add_query('INSERT', $this->prefix.'extension_groups', "6,'Streams',2,0,1,'',0,''");
			$this->add_query('INSERT', $this->prefix.'extension_groups', "7,'Flash Files',3,0,1,'',0,''");
			# -- extensions
			$this->add_query('INSERT', $this->prefix.'extensions', "1, 1,'gif', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "2, 1,'png', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "3, 1,'jpeg', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "4, 1,'jpg', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "5, 1,'tif', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "6, 1,'tga', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "7, 2,'gtar', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "8, 2,'gz', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "9, 2,'tar', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "10, 2,'zip', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "11, 2,'rar', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "12, 2,'ace', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "13, 3,'txt', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "14, 3,'c', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "15, 3,'h', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "16, 3,'cpp', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "17, 3,'hpp', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "18, 3,'diz', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "19, 4,'xls', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "20, 4,'doc', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "21, 4,'dot', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "22, 4,'pdf', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "23, 4,'ai', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "24, 4,'ps', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "25, 4,'ppt', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "26, 5,'rm', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "27, 6,'wma', ''");
			$this->add_query('INSERT', $this->prefix.'extensions', "28, 7,'swf', ''");
			# -- default quota limits
			$this->add_query('INSERT', $this->prefix.'quota_limits', "1, 'Low', 262144");
			$this->add_query('INSERT', $this->prefix.'quota_limits', "2, 'Medium', 2097152");
			$this->add_query('INSERT', $this->prefix.'quota_limits', "3, 'High', 5242880");
		}

		$this->add_query('INSERT', $this->prefix.'topic_icons', '"1", "-1", "images/icons/misc/asterix.gif", "asterix"');
		$this->add_query('INSERT', $this->prefix.'topic_icons', '"2", "-1", "images/icons/misc/arrow_bold_ltr.gif", "Arrow ltr"');
		$this->add_query('INSERT', $this->prefix.'topic_icons', '"3", "-1", "images/icons/smile/exclaim.gif", "Exclamation"');
		$this->add_query('INSERT', $this->prefix.'topic_icons', '"4", "-1", "images/icons/smile/question.gif", "Questionmark"');
		$this->add_query('INSERT', $this->prefix.'topic_icons', '"5", "-1", "images/icons/smile/idea.gif", "Idea"');
		return true;
	}

	function pre_uninstall()  { return true; }
	function post_uninstall() { return true; }
	function pre_upgrade($prev_version)
	{
		if (version_compare($prev_version, '3.0.4', '<'))
		{
			global $db;
			$db->query("UPDATE {$db->TBL->bbforums} SET prune_next=0 WHERE prune_next IS NULL");
			$db->query("UPDATE {$db->TBL->bbtopics} SET icon_id=0 WHERE icon_id IS NULL");
		}
		return true;
	}
	function post_upgrade($prev_version)
	{
		global $db;
		if (version_compare($prev_version, '3.0.4', '<') && isset($db->TBL->bbforum_archive)) {
			$result = $db->query("SELECT forum_id, archive_days, archive_freq FROM {$db->TBL->bbforum_archive}");
			while ($row = $result->fetch_row()) {
				$db->query("UPDATE {$db->TBL->bbforums} SET archive_days={$row[1]}, archive_freq={$row[2]} WHERE forum_id={$row[0]}");
			}
			$db->query("DROP TABLE {$db->TBL->bbforum_archive}");
			$result = $db->query("SELECT forum_id, prune_days, prune_freq FROM {$db->TBL->bbforum_prune}");
			while ($row = $result->fetch_row()) {
				$db->query("UPDATE {$db->TBL->bbforums} SET prune_days={$row[1]}, prune_freq={$row[2]} WHERE forum_id={$row[0]}");
			}
			$db->query("DROP TABLE {$db->TBL->bbforum_prune}");
		}
		return true;
	}

}
